/**
 * Created by quliang on 16-4-7 下午2:07. Copyright (c) 2016 SAIZ.CN. All rights reserved.
 */

package com.example.db.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.view.Display;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public final class CommonUtil {
	/**
	 * 获得当前屏幕长/宽尺寸
	 */
	public static Point getScreenSize(Activity activity) {
		Point size = new Point();
		if (activity == null) {
			return size;
		}
		getDefaultDisplay(activity).getSize(size);
		return size;
	}

	/**
	 * 将驼峰风格替换为下划线风格
	 */
	public static String toUnderline(String str) {
		Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
		StringBuilder builder = new StringBuilder(str);
		for (int i = 0; matcher.find(); i++) {
			builder.replace(matcher.start() + i, matcher.end() + i, "_"
					+ matcher.group().toLowerCase());
		}
		if (builder.charAt(0) == '_') {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	/**
	 * 将下划线风格替换为驼峰风格
	 */
	public static String toHump(String str) {
		Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
		StringBuilder builder = new StringBuilder(str);
		for (int i = 0; matcher.find(); i++) {
			builder.replace(matcher.start() - i, matcher.end() - i, matcher
					.group().substring(1).toUpperCase());
		}
		if (Character.isUpperCase(builder.charAt(0))) {
			builder.replace(0, 1,
					String.valueOf(Character.toLowerCase(builder.charAt(0))));
		}
		return builder.toString();
	}

	public static int dip2px(float dpValue) {
		final float scale = AppUtil.getContext().getResources()
				.getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private static Display getDefaultDisplay(Activity activity) {
		return activity.getWindowManager().getDefaultDisplay();
	}

	public static void msg(int id) {
		Toast.makeText(AppUtil.getContext(), id, Toast.LENGTH_SHORT).show();
	}

	public static void msg(String msg) {
		Toast.makeText(AppUtil.getContext(), msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * @author liLI 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * LI 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 对图片进行等比缩减
	 */
	public static Bitmap BitmapToSmaill(Bitmap bitmap, int size) {
		Bitmap Smallbitmap = null;
		Matrix m = new Matrix();
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		if (width > size || height > size) {
			float ScrWidth;
			if (width > height) {
				ScrWidth = (float) size / width;
			} else {
				ScrWidth = (float) size / height;
			}
			m.postScale(ScrWidth, ScrWidth); // 长和宽放大缩小的比例
		}
		Smallbitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), m, true);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Smallbitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
		Smallbitmap = BitmapFactory.decodeStream(isBm, null, null);
		return Smallbitmap;
	}

	public static boolean getJsonElementIsObjectByString(String json) {
		if(json==null||json.equals(""))
		{
			return false;
		}
		if (JsonUtil.getJsonElement(json).isJsonArray()) {
			return false;
		}
		HashMap<String, Object> map = JsonUtil.decode(json, HashMap.class);
		if (map.containsKey("result")
				&& ObjectUtil.isEquals(map.get("result").toString(), "-1.0")) {
			return true;
		}
		return false;
	}
}
